package com.orm.mybatis.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.*;

public class SendMail {
    //    @Autowired
//    private UserService userService;
//    private static RestTemplate restTemplate ;
    private  static String url = "http://10.132.241.214:6666/mail";
    private  static String token = "25df2154fg105p356120wlks12542fv0";
    public static void send(String subject) {
        RestTemplate restTemplate = new RestTemplate();
//      String url = "http://10.132.241.214:6666/mail";
        HttpHeaders headers = new HttpHeaders();
//      headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Content-type","application/json");
        String[] strings = {"andrew.wq.liu@mail.foxconn.com"};
        Map<String,Object> map2 = new HashMap<>();
        map2.put("rcpts",strings);
        map2.put("subject",subject);
        map2.put("content","2320");
        JSONObject map3 = new JSONObject();
        map3.put("data",map2);
        map3.put("token" , "25df2154fg105p356120wlks12542fv0");
        System.out.println(map3);
        //将请求头部和参数合成一个请求
        HttpEntity<JSONObject> request = new HttpEntity<>(map3, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        System.out.println(response.getBody());
    }

    public static String toSend(List<String> strings, String subject, String content, JSONArray jsonArray) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type","application/json");
//        List<String> list = Arrays.asList(strings);
        HashSet<String> hashSet = new HashSet<>(strings);
        String[] strs = hashSet.toArray(new String[hashSet.size()]);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("rcpts",strs);
        map.put("subject",subject);
        map.put("content",content);
        map.put("attach",jsonArray);
        JSONObject map2 = new JSONObject();
        map2.put("data",map);
        map2.put("token" , token);
        //String string = JSON.toJSONString(map2);
        //将请求头部和参数合成一个请求
        HttpEntity<JSONObject> request = new HttpEntity<>(map2, headers);
        System.out.println(request.toString());
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        return response.getBody();
        //System.out.println(response.getBody());
        //return "success";
    }

    public static void main(String[] args) {
//        String[] a  = {"andrew.wq.liu@mail.foxconn.com"};
        List<String> a =  new ArrayList<>();
        a.add("andrew.wq.liu@mail.foxconn.com");
        String contenting = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" >\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=7\" />\n" +
                "\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\" />\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\" />\n" +
                "  <title> 辦公自動化平臺_電子簽核系統</title>\n" +
                "</head>\n" +
                "<body style=\"padding: 0;margin: 0;font-family: Microsoft YaHei, Segoe UI, Lucida Grande, Arial, Helvetica, sans-serif; position: relative;height: 100%;width: 100%\">\n" +
                "  <table style=\"position: relative;height: 100%;width: 100%;color: #333;\" cellpadding=\"0\">\n" +
                "    <tr>\n" +
                "      <td style=\"background: #1d82d2;color: #fff;font-size: 24px;height: 65px;line-height: 65px;padding-left: 20px;\"><img src=\"http://ackh.efoxconn.com/smart-factory/static/images/white-logo.png\" style=\"vertical-align: -10%\">電子簽核系統</td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "      <td style=\"padding-left: 18px\">\n" +
                "        <p style=\"line-height: 60px\">尊敬的<span>#{applicant_name}</span>（主管）：</p>\n" +
                "        <p style=\"line-height: 60px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好！<span>劉翠</span>邀請您審核 <span>ASUS T專案組裝段夾治具緊急採購單-更新版</span>，請點擊<a href=http:&#47;&#47;ackh.efoxconn.com&#47;smart-factory&#47;approvals&#47;26982?mail_token=approval-a669656b-29db-4c67-852e-f90bf127e47f style=\"line-height: 60px;color: #1d82d2\">鏈接</a>查看詳情，如非本人請略過此郵件，謝謝！</p>\n" +
                "\n" +
                "        <p style=\"line-height: 30px;font-size: 12px;color:#999;margin-top: 15px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;（該鏈接有效期24小時)</p>\n" +
                "        <a href=http:&#47;&#47;ackh.efoxconn.com&#47;smart-factory&#47;auth&#47;login?Lw== style=\"margin-left: 35px;line-height: 60px;color: #1d82d2\">如以上鏈接失效，請點擊此處登錄</a>\n" +
                "        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;感謝您使用電子簽核系統，祝您使用愉快，謝謝！</p>\n" +
                "        <p style=\"float: right;font-size: 14px;color: #333;margin-right: 160px;margin-top: 80px;\">系統作業時間：2021-10-18 09:09:41</p>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "      <td style=\"background: #464646;width:100%;height: 45px;line-height: 45px;color:#999;padding-left: 18px;\">\n" +
                "        <span style=\"font-size:12px;\">此郵件為系統郵件，請勿直接回復</span>\n" +
                "        <span style=\"font-size:20px;float: right;margin-right: 10px\">電子簽核系統</span>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "  </table>\n" +
                "</body>\n" +
                "</html>";
        String content_finish = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n" +
                "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" style=\"position: relative;height: 100%;width: 100%\n" +
                "\">\n<head>\n<meta charset=\"UTF-8\">\n<title> 辦公自動化平臺_電子簽核系統</title>\n</head>\n<body style=\"padding: 0;margin: 0;font-family: Microsoft YaHei, Segoe \n" +
                "UI, Lucida Grande, Arial, Helvetica, sans-serif; position: relative;height: 100%;width: 100%\">\n<table style=\"position: relative;height: 100%;width: 100%;color: \n" +
                "#333;\" cellpadding=\"0\">\n<tr>\n<td style=\"background: #1d82d2;color: #fff;font-size: 24px;height: 65px;line-height: 65px;padding-left: 20px;\"><img src=\n" +
                "\"http://ackh.efoxconn.com/smart-factory/static/images/white-logo-3c80c89e8d.png\" style=\"vertical-align: -10%\">電子簽核系統</td>\n</tr>\n<tr>\n\n" +
                "<td style=\"padding-left: 18px\">\n<p style=\"line-height: 60px\">尊敬的<span>{applicant_name}</span>（主管）：</p>\n<p style=\"line-height: 60px\n" +
                "\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好！您於 <span>#{create_at}</span>提交的 <span>{subject}</span>審批完成。請點擊<a \n" +
                "href=http:&#47;&#47;ackh.efoxconn.com&#47;smart-factory&#47;approvals&#47;#{id}?mail_token=approval-UUID style=\"line-height: 60px;color: \n" +
                "#1d82d2\">鏈接</a>查看詳情，如非本人請略過此郵件，謝謝！</p>\n<p style=\"line-height: 30px;font-size: 12px;color:#999;margin-top: 15px;\n" +
                "\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;（該鏈接有效期24小時)</p>\n<a href=http:&#47;&#47;ackh.efoxconn.com&#47;smart-factory&#47;auth&#47;login \n" +
                "style=\"margin-left: 35px;line-height: 60px;color: #1d82d2\">如以上鏈接失效，請點擊此處登錄</a>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;感謝您使用電子簽核\n" +
                "系統，祝您使用愉快，謝謝！</p>\n<p style=\"float: right;font-size: 14px;color: #333;margin-right: 160px;margin-top: 80px;\">系統作業時間：#{SYSTEM_TIME}<\n" +
                "/p>\n</td>\n</tr>\n<tr>\n<td style=\"background: #464646;width:100%;height: 45px;line-height: 45px;color:#999;padding-left: 18px;\">\n<span \n" +
                "style=\"font-size:12px;\">此郵件為系統郵件，請勿直接回復</span>\n<span style=\"font-size:20px;float: right;margin-right: 10px\">電子簽核系統</span>\n<\n" +
                "/td>\n</tr>\n</table>\n</body>\n</html>";
        Map<String,String> map = new HashMap<String, String>();
        map.put("attach_name","20211008-00008.pdf");
//        hashMap.put("attach_name","企鹅桌面V7.0-操作手册.pdf");
//        hashMap.put("attach_name","1231231231.pfd");
        String base64 =  PDFFileUtils.PDFToBase64("D:/3/20211008-00008.pdf");
        String base64_pdf = base64.trim().replaceAll("\\s*|\t|\r|\n", "");
//        hashMap.put("attach_content",base64);
        map.put("attach_content",base64_pdf);
//        List<HashMap> hashMapList = new ArrayList<>();
        JSONArray hashMapList = new JSONArray();
        hashMapList.add(map);
       SendMail.toSend(a,"【電子簽核系統】IT10月排班申請 審批完成通知！",content_finish,hashMapList);
       System.out.println(a);
       System.out.println(content_finish);
       System.out.println(hashMapList.toString().substring(0,200));
    }

}
